/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -04 -11
 * Time: 14:08
 */
public class LinkedList {
    class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;
    private ListNode tail;
    private int size;

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = new ListNode(data);
            tail = head;
        } else {
            head.prev = node;
            node.next = head;
            head = node;
        }
        size++;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = new ListNode(data);
            tail = head;
        } else {
            tail.next = node;
            node.prev = tail;
            tail = node;
        }
        size++;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index, int data) {
        if (index < 0 || index > size) return false;
        if (index == 0) {
            addFirst(data);
        } else if (index == size) {
            addLast(data);
        } else {
            int cnt = 1;
            ListNode cur = head;
            while (cnt != index) {
                cnt++;
                cur = cur.next;
            }
            ListNode node = new ListNode(data);
            ListNode next = cur.next;
            cur.next = node;
            node.prev = cur;
            node.next = next;
            next.prev = node;
            size++;
        }
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                ListNode prev = cur.prev;
                ListNode next = cur.next;
                if (prev != null) {
                    prev.next = next;
                }
                if (next != null) {
                    next.prev = prev;
                }
                if (cur == head) {
                    head = next;
                }
                if (cur == tail) {
                    tail = prev;
                }
                size--;
                return;
            }
            cur = cur.next;
        }
    }


    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                ListNode prev = cur.prev;
                ListNode next = cur.next;
                if (prev != null) {
                    prev.next = next;
                }
                if (next != null) {
                    next.prev = prev;
                }
                if (cur == head) {
                    head = next;
                }
                if (cur == tail) {
                    tail = prev;
                }
                size--;
            }
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size() {
        return size;
    }


    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val);
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        head = tail = null;
        size = 0;
    }

    public static void main(String[] args) {
        LinkedList list=new LinkedList();
        list.addLast(1);
        list.addLast(1);
        list.addLast(1);
        list.addLast(1);
        list.addLast(1);
        list.addLast(1);
        list.addFirst(2);
        list.addFirst(2);
        list.addFirst(2);
        list.addFirst(2);
        list.addFirst(2);
        list.addIndex(5,7);
        list.addIndex(list.size(),9);
        list.display();
        list.removeAllKey(1);
        list.display();
        list.remove(9);
        list.display();
        System.out.println(list.size());
        list.remove(2);
        list.display();
        list.removeAllKey(2);
        System.out.println(list.size());
        list.display();
    }
}